package com.strelitzia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.strelitzia.dao.MusicDao;
import com.strelitzia.entity.MusicEntity;
import com.strelitzia.exception.StrelitziaException;
import com.strelitzia.filter.CorsFilter;
import com.strelitzia.service.MusicService;
import com.strelitzia.utils.FileUtil;
import com.strelitzia.vo.MusicForm;
import com.strelitzia.vo.MusicItem;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @create 2024/1/19 11:01
 */
@Service
public class MusicServiceImpl extends ServiceImpl<MusicDao, MusicEntity> implements MusicService {
    @Override
    public void add(MultipartFile file) throws StrelitziaException {
        MusicEntity musicEntity = new MusicEntity();
        if (file != null && !"".equals(file.getOriginalFilename())) {
            musicEntity.setTitle(setTitle(Objects.requireNonNull(file.getOriginalFilename())));
            musicEntity.setUid(CorsFilter.loginUserId.get());
            String src = FileUtil.fileUp(file);
            if (!src.equals("")) {
                musicEntity.setUrl(src);
                this.save(musicEntity);
            } else {
                throw new StrelitziaException("上传歌曲失败");
            }
        }
    }

    @Override
    public void copy(Integer id) throws StrelitziaException {
        MusicEntity byId = this.getById(id);
        if (byId != null) {
            MusicEntity musicEntity = new MusicEntity();
            musicEntity.setUrl(byId.getUrl());
            musicEntity.setTitle(setTitle(byId.getTitle()));
            musicEntity.setUid(CorsFilter.loginUserId.get());
            this.save(musicEntity);
        }else {
            throw new StrelitziaException("该歌曲不存在");
        }

    }

    @Override
    public List<MusicItem> getList() {
        List<MusicEntity> list = this.list(new QueryWrapper<MusicEntity>()
                .eq("uid", CorsFilter.loginUserId.get())
                .orderByDesc("weight"));
        if (list != null) {
            return list.stream().map(item -> {
                        MusicItem musicItem = new MusicItem();
                        musicItem.setTitle(item.getTitle());
                        musicItem.setUrl("/api/res/" + item.getUrl());
                        musicItem.setId(item.getId());
                        musicItem.setIndex(item.getWeight());
                        return musicItem;
                    }
            ).collect(Collectors.toList());
        } else {
            return new ArrayList<>();

        }

    }

    @Override
    public List<MusicItem> getList(Integer id) {
        List<MusicEntity> list = this.list(new QueryWrapper<MusicEntity>()
                .eq("uid", id));
        if (list != null) {
            return list.stream().map(item -> {
                        MusicItem musicItem = new MusicItem();
                        musicItem.setTitle(item.getTitle());
                        musicItem.setUrl("/api/res/" + item.getUrl());
                        musicItem.setId(item.getId());
                        musicItem.setIndex(item.getWeight());
                        return musicItem;
                    }
            ).collect(Collectors.toList());
        } else {
            return new ArrayList<>();

        }
    }

    @Override
    public void edit(MusicForm musicForm) {
        MusicEntity musicEntity = new MusicEntity();
        musicEntity.setId(musicForm.getId());
        musicEntity.setTitle(setTitle(musicForm.getTitle()));
        this.updateById(musicEntity);
    }

    public String setTitle(String documentName) {

        MusicEntity one = this.getOne(new QueryWrapper<MusicEntity>().eq("uid", CorsFilter.loginUserId.get())
                .eq("title", documentName));
        if (one == null) {
            return documentName;
        } else {
            int i = documentName.lastIndexOf(".");
            String fileName = documentName.substring(0, i);
            String hzName = documentName.substring(i);
            int index = 1;
            String[] split = fileName.split("\\(");
            if (split.length > 0) {
                String str = split[split.length - 1].replace(")", "");
                try {
                    index = Integer.parseInt(str);
                    index = index + 1;
                    int a = fileName.lastIndexOf("(");
                    fileName = fileName.substring(0, a);
                } catch (NumberFormatException e) {
                    // 忽略异常
                }
            }
            return setTitle(fileName + "(" + index + ")" + hzName);
        }
    }
}
